package com.cvv.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cvv.dao.SupplierMapper;
import com.cvv.entity.Supplier;

@Controller
@RequestMapping("suapi")
public class SupplierController {

	@Autowired
	private SupplierMapper suppliermapper;
	
	@RequestMapping("getsubypage")
	public String getsubypage(Model model,HttpServletRequest request){
		String currentIndex = request.getParameter("pageIndex");
		String path = request.getParameter("path");
		if(path==null) {
			path = "../dist_hub/provide/select_provide.jsp";
		}
		int pageSize = 6;
		int pageIndex = 1;
		if(currentIndex!=null) {
			pageIndex=Integer.parseInt(currentIndex);
		}
		List<Supplier> listsu = suppliermapper.getsubypage((pageIndex-1)*pageSize, pageSize);
		model.addAttribute("pageIndex", pageIndex);
		int count = suppliermapper.getTotalPage();
		int totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("listsu", listsu);
		String p = "forward:"+path+"";
		return p;
	}
	
	@RequestMapping("getsubyid")
	public String getsubyid(Model model,HttpServletRequest request) {
		int providerid = Integer.valueOf(request.getParameter("providerid"));
		Supplier supplier = suppliermapper.getsubyid(providerid);
		model.addAttribute("supplier", supplier);
		return "forward:../dist_hub/provide/show_provide.jsp";
	}
	
	@RequestMapping("delsubyid")
	public String delsubyid(Model model,HttpServletRequest request){
		int providerid = Integer.valueOf(request.getParameter("providerid"));
		int num = suppliermapper.delsupplier(providerid);
		return "redirect:/suapi/getsubypage";
	}
	
	@RequestMapping("linkupdate")
	public String linkupdate(Model model,final int tex1) {
		Supplier supplier = suppliermapper.getsubyid(tex1);
		model.addAttribute("supplier", supplier);
		return "forward:../dist_hub/provide/edit_provide.jsp";
	}
	
	@RequestMapping("updatesubyid")
	public String updatesubyid(Model model,final int tex1,final String tex2,final String tex3,final int tex4,final String tex5,final String tex6,final String tex7,final String tex8) {
        Supplier supplier = new Supplier(tex1,tex2,tex7,tex5,tex6,tex4,tex3,tex8);
        int num = suppliermapper.updatesubyid(supplier);
		return "redirect:/suapi/getsubypage";
	}
	
	@RequestMapping("addsupplier")
	public String addsupplier(Model model,final int tex1,final String tex2,final String tex3,final int tex4,final String tex5,final String tex6,final String tex7,final String tex8) {
       System.out.println("aaa");
		Supplier supplier = new Supplier(tex1,tex2,tex7,tex5,tex6,tex4,tex3,tex8);
        int num = suppliermapper.addsu(supplier);
		return "redirect:/suapi/getsubypage";
	}
}
